package com.mentalhealthplatform.global.log.login;


import com.mentalhealthplatform.global.log.dto.LoginLogDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;

public interface LoginLogRepository extends JpaRepository<LoginLog, Long>, JpaSpecificationExecutor<LoginLog> {
    void deleteByIdIn(Long[] ids);

    @Query("SELECT new com.mentalhealthplatform.global.log.dto.LoginLogDTO(" +
            "l.id, l.loginName, l.loginAddress, l.loginLocation, l.browser, l.operatingSystem, " +
            "l.loginStatus, l.loginMessage, l.loginTime) " +
            "FROM LoginLog l " +
            "WHERE (:loginName IS NULL OR l.loginName LIKE %:loginName%) " +
            "AND (:loginStatus IS NULL OR l.loginStatus = :loginStatus) " +
            "AND (:loginAddress IS NULL OR l.loginAddress LIKE %:loginAddress%) " +
            "AND (:loginLocation IS NULL OR l.loginLocation LIKE %:loginLocation%) " +
            "AND (:browser IS NULL OR l.browser LIKE %:browser%) " +
            "AND (:operatingSystem IS NULL OR l.operatingSystem LIKE %:operatingSystem%) " +
            "AND (:startDate IS NULL OR l.loginTime >= :startDate) " +
            "AND (:endDate IS NULL OR l.loginTime <= :endDate)")
    Page<LoginLogDTO> findLoginLogsWithFilters(@Param("loginName") String loginName,
                                               @Param("loginStatus") String loginStatus,
                                               @Param("loginAddress") String loginAddress,
                                               @Param("loginLocation") String loginLocation,
                                               @Param("browser") String browser,
                                               @Param("operatingSystem") String operatingSystem,
                                               @Param("startDate") LocalDateTime startDate,
                                               @Param("endDate") LocalDateTime endDate,
                                               Pageable pageable);


}
